package com.jackyzgy.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;

public class ListCompare {

	public static void main(String[] args) {

		List<Person> listA = new ArrayList<Person>();

		Person p1 = new Person();
		Person p2 = new Person();
		Person p3 = new Person();
		
		Date date = DateUtils.addDays(new Date(), 40);

		p1.setName("name1");
		p1.setOrder(4);
		p1.setDate(date);
		System.out.println(date);

		p2.setName("name2");
		p2.setOrder(2);
		date = DateUtils.addDays(date, 1);
		p2.setDate(date);
		System.out.println(date);

		p3.setName("name3");
		p3.setOrder(3);
		date = DateUtils.addDays(date, -3);
		p3.setDate(date);
		System.out.println(date);

		listA.add(p2);
		listA.add(p1);
		listA.add(p3);

		Collections.sort(listA, new Comparator<Person>() {
			public int compare(Person arg0, Person arg1) {
				int order = arg0.getDate().compareTo(arg1.getDate());
				System.out.println(order);
				return order;
			}
		});

		for (Person p : listA) {
			System.out.println(p.getName());
		}

	}
}

class Person {

	private String name;

	private Integer order;
	
	private Date date;

	/**
	 * 
	 * @return the name
	 */

	public String getName() {

		return name;

	}

	/**
	 * 
	 * @param name
	 * 
	 *            the name to set
	 */

	public void setName(String name) {

		this.name = name;

	}

	/**
	 * 
	 * @return the order
	 */

	public Integer getOrder() {

		return order;

	}

	/**
	 * 
	 * @param order
	 * 
	 *            the order to set
	 */

	public void setOrder(Integer order) {

		this.order = order;

	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Date getDate() {
		return date;
	}

}
